package com.cskaoyan.service.kylan;

import com.cskaoyan.bean.Keyword;
import com.cskaoyan.bean.KeywordExample;
import com.cskaoyan.bean.SearchHistory;
import com.cskaoyan.bean.SearchHistoryExample;
import com.cskaoyan.bean.wxSearch.SearchIndexVO;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.mapper.SearchHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    KeywordMapper keywordMapper;

    @Autowired
    SearchHistoryMapper historyMapper;

    /**
     * 搜索框显示默认搜索，热门搜索，历史搜索；
     * 涉及表格：keyword，history
     *
     * @return
     */
    @Override
    public SearchIndexVO getKeyWords() {

        KeywordExample keywordExample = new KeywordExample();

        //获取defaultKeyword
        keywordExample.createCriteria().andDeletedEqualTo(false).andIsDefaultEqualTo(true);
        List<Keyword> defaultKeywordList = keywordMapper.selectByExample(keywordExample);
        Keyword defaultKeyword = defaultKeywordList.get(0);
        Integer defaultKeywordId = defaultKeyword.getId();

        //获取hotKeywordList
        keywordExample.clear();
        keywordExample.createCriteria().andDeletedEqualTo(false).andIsHotEqualTo(false);
        List<Keyword> hotKeyWordList = keywordMapper.selectByExample(keywordExample);


        //获取前一天的时间
        Date dateNow = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateNow);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date dateBefore = calendar.getTime();

        //获取historyKeywordList
        SearchHistoryExample historyExample = new SearchHistoryExample();
        historyExample.createCriteria().andDeletedEqualTo(false);
        historyExample.setOrderByClause("update_time desc");
        List<SearchHistory> historyList = historyMapper.selectByExample(historyExample);

        List<SearchHistory> historyKeywordList = new ArrayList<>();
        for (SearchHistory history : historyList) {
            historyKeywordList.add(history);
        }


        //赋值，返回
        SearchIndexVO searchIndexVO = new SearchIndexVO();
        searchIndexVO.setDefaultKeyword(defaultKeyword);
        searchIndexVO.setHotKeywordList(hotKeyWordList);
        searchIndexVO.setHistoryKeywordList(historyKeywordList);

        return searchIndexVO;
    }

    /**
     * 获取历史搜索提示
     * @param keyword
     * @return
     */
    @Override
    public List<String> getKeywordsHelper(String keyword) {
        SearchHistoryExample historyExample = new SearchHistoryExample();
        historyExample.createCriteria().andKeywordLike("%" + keyword + "%").andDeletedEqualTo(false);

        List<SearchHistory> historyList = historyMapper.selectByExample(historyExample);

        List<String> keywordList = new ArrayList<>();
        for (SearchHistory history : historyList) {
            keywordList.add(history.getKeyword());
        }

        return keywordList;
    }

    /**
     * 删除搜索历史：将deleted置一；
     * 涉及表格：history
     * @return
     */
    @Override
    public Integer deleteHistorySearch() {

        SearchHistoryExample historyExample = new SearchHistoryExample();
        historyExample.createCriteria().andDeletedEqualTo(false);

        SearchHistory searchHistory = new SearchHistory();
        searchHistory.setDeleted(true);
        Integer affectedRows = historyMapper.updateByExampleSelective(searchHistory, historyExample);

        return affectedRows;

    }

}
